﻿@page "/CPUMonitor"

<h1>CPUMonitor</h1>

@if (info == null)
{
    if (Environment.OSVersion.Platform == PlatformID.Unix)
    {
        info = new UnixCPUInfo();
    }
    else
    {
        //<div>Only for Unix currently.</div>
        //return;

        info = new DebugCPUInfo();

    }
}

@if (info is DebugCPUInfo)
{
    <div>In windows , the code show testing data only.</div>
}

<div> CPU Name : @info.GetCPUName() , Temperature : @Math.Floor(info.GetCPUTemp())°C , Load : @lastused% Frequence : @info.GetCPUFreq()</div>

<BlazorDomTree OnRootReady="bdt_ready" />

@code {

    CPUInfo info;

    int lastused = 0;

    class CPUState
    {
        public int Idle;
        public int User;
        public int System;
    }
    abstract class CPUInfo
    {
        string _cpuname;
        public string GetCPUName()
        {
            if (_cpuname == null)
                _cpuname = GetCPUNameImpl();
            return _cpuname;
        }
        protected abstract string GetCPUNameImpl();
        public abstract double GetCPUTemp();
        public abstract CPUState[] GetCPUStates();
        public virtual int GetCPUFreq() { return -1; }
    }
    class DebugCPUInfo : CPUInfo
    {
        protected override string GetCPUNameImpl()
        {
            return "Debug";
        }
        Random r = new Random();
        public override double GetCPUTemp()
        {
            return 40 + Math.Sin(DateTime.Now.TimeOfDay.TotalSeconds / 5) * 10 + r.NextDouble() * 2;
        }

        CPUState _prevstate;
        DateTime _prevtime;
        public override CPUState[] GetCPUStates()
        {
            if (_prevstate == null || DateTime.Now - _prevtime > TimeSpan.FromMilliseconds(900))
            {
                CPUState newstate = new CPUState();
                if (_prevstate != null)
                {
                    newstate.Idle = _prevstate.Idle;
                    newstate.User = _prevstate.User;
                    newstate.System = _prevstate.System;
                }
                double adduser = 20 + Math.Sin(DateTime.Now.TimeOfDay.TotalSeconds / 5) * 20 + r.NextDouble() * 2;
                double addsystem = 15 + Math.Sin(DateTime.Now.TimeOfDay.TotalSeconds / 4) * 15 + r.NextDouble() * 12;
                newstate.User += (int)adduser;
                newstate.System += (int)addsystem;
                newstate.Idle += 100 - (int)adduser - (int)addsystem;
                _prevstate = newstate;
                _prevtime = DateTime.Now;
            }
            return new CPUState[] { _prevstate };
        }
    }
    class UnixCPUInfo : CPUInfo
    {
        protected override string GetCPUNameImpl()
        {
            foreach (string line in System.IO.File.ReadAllLines("/proc/cpuinfo"))
            {
                string[] pair = line.Split(':');
                if (pair[0].Trim() == "Hardware")
                    return pair[1].Trim();
            }
            return "(unknown)";
        }
        public override double GetCPUTemp()
        {
            return double.Parse(System.IO.File.ReadAllText("/sys/class/thermal/thermal_zone0/temp").Trim()) / 1000;
        }
        public override CPUState[] GetCPUStates()
        {
            string alltext = System.IO.File.ReadAllText("/proc/stat");
            List<CPUState> states = new List<CPUState>();
            foreach (string eachline in alltext.Split('\n'))
            {
                string[] arr = eachline.Split(' ', '\t');
                string name = arr[0].Trim();
                if (name.Length < 4 || !name.StartsWith("cpu"))
                    continue;
                CPUState state = new CPUState();
                states.Add(state);
                state.Idle = int.Parse(arr[4]);
                state.User = int.Parse(arr[1]);
                state.System = int.Parse(arr[3]);
            }
            return states.ToArray();
        }
        public override int GetCPUFreq()
        {
            List<int> list = new List<int>();
            for (int i = 0; true; i++)
            {
                string path = "/sys/devices/system/cpu/cpu"+i+"/cpufreq/cpuinfo_cur_freq";
                if (!System.IO.File.Exists(path))
                    break;
                list.Add(int.Parse(System.IO.File.ReadAllText(path).Trim()) / 1000);
            }
            return (int)list.Average();
        }
    }

    void bdt_ready(BlazorDomTree bdt)
    {
        bool isdisposed = false;
        bdt.Disposed += delegate
        {
            isdisposed = true;
        };

        int priod = 500;
        int maxcount = 120;
        double width = 360;
        var tempcanvas = bdt.Root.Create<PlusCanvas>("width=" + width + " height=200 style='background-color:#eee'");

        bdt.Root.Create("br");
        PlusControl btm = bdt.Root.Create("div");
        PlusControl label = btm.Create("label");
        PlusControl checkbox = label.Create("input type='checkbox'");
        checkbox.Attribute_Checked(true);
        label.Create("span").InnerText("Alert when temperature > than");
        int alertvalue = 50;
        PlusControl inptemp = btm.Create("input type=number style='width:60px'").Value(alertvalue.ToString());
        inptemp.OnChange(delegate
        {
            int dv;
            if (int.TryParse(inptemp.Value, out dv))
                alertvalue = dv;
            else
                BlazorSession.Current.Alert("Error", "InvalidValue", () => inptemp.SetFocus());
        });


        PlusControl audio = new PlusControl("audio");
        string dataurl = "data:audio/mpeg;base64,SUQzAwAAAAAAEFRDT04AAAAGAAAAQmx1ZXP/8sDAM08AWkReSU1a0AAxERIv4/j+KkWIuyMRiMOWw9U6713rvSHSLae4AMBGMJGYNGYKAIomSZNaceafjsfbceCgcBcZwYnggLMGFMODMQEMEALsLofx2FyF/C2hhQZhxJixpjyJkSJjxpiw4CDoLqCJeFwAAAMECMAALiM0cMu4W0LwIqJiLEZw5DkM7a+5bvw/L6evT0lJhuvT0+UYct34fl9PTyuNw+/kYjEYjEYjEYlkbjcNu25bO2HsTcdrCcgIAGGFGMHGQHGIAKlMWVNgyNgoM8SARBOiHIw7DOFjrvceflD+OQxB+L+H/+dPK5fGIYdhrDOGcNcfi9SUmMrf9y3/cty2IM4dyMUlJhjXjEMP4/j+P5DkspKfPP//CpSU8bf923/h+n7+ef4UlJScrww/j+P4/kOSzGVuQ5D+Syx/dShy3Lf+H5fnqpGH/h/KGHYciHL0Ns4a5FLsNuW5cXmH/f9/42/7/w/L8qen7UpKSkscaFYwHA4HAwGAwGAgDAXiuyYGADSNjMrAwzgEDT6rN88EICJiY6GZQgb/8sLAyUopZAxOil+c8AKdJBjUAskDEVOmI2IQa79apnDK3u0YEAFSQ0YMCcDASAAMnRUU23l2TMiBpX0yN/H2g9xTEsClMPsQ4x3RiDBqLV+5HH7pbD6R4w3A+jDtCWME0H4wMAmzDCGPb57IxDtylmo5IumFuGUYNAABgDARGBUACAgUjAdAZMAkAGVOtILOdy1d5Rfm2SRgoEAwEQOjAQAsMAAAUIA1MBABJoS8DAqALxrOXamZ3UYw1jDFrvzNXzAkAJWwYEYNBh/iEGdkQKZKhCphqAYmAOCeYGoTxhXgkAEBcwAwKUUcN9nC2ThRuWYdqY1v/s3DFrf6q871CElAIaGwFB0kA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qqqqqqqqqqqqqqqqqqqqqqqqqqqqo=";

        audio.SetAttribute("src", dataurl);

        void DoAlert()
        {
            BlazorSession.Current.Toast("Warning " + info.GetCPUTemp(), 1000, "cputemp");
            audio.Eval("this.play();");
        }

        btm.Create("button").InnerText("Play now").OnClick(delegate
        {
            DoAlert();
        });


        bdt.Root.Create("hr");

        var loadcanvas = bdt.Root.Create<PlusCanvas>("width=" + width + " height=200 style='background-color:#eee'");

        bdt.Root.Create("br");

        string cpuloaduid = Guid.NewGuid().ToString();

        bdt.Root.Create("button").InnerText("Add Cpu Load").OnClick(delegate
        {
            string uid = cpuloaduid;
            var t = new System.Threading.Thread(() =>
            {
                while (uid == cpuloaduid && !isdisposed)
                    ;
            });
            t.Start();
        });
        bdt.Root.Create("button").InnerText("Clear Cpu Load").OnClick(delegate
        {
            cpuloaduid = Guid.NewGuid().ToString();
        });
        bdt.Root.Create("button").InnerText("Test Single Core").OnClick(delegate
        {
            cpuloaduid = Guid.NewGuid().ToString();
            var bs = BlazorSession.Current;
            var t = new System.Threading.Thread(() =>
            {
                var watch = new System.Diagnostics.Stopwatch();
                int fib(int n)
                {
                    if (n < 3)
                        return 1;
                    return fib(n - 1) + fib(n - 2);
                }
                watch.Start();
                fib(40);
                watch.Stop();
                bs.InvokeInRenderThread(delegate
                {
                    bs.Alert("Result", Convert.ToString(Math.Floor(2000 / watch.Elapsed.TotalSeconds)));
                });
            });
            t.Start();
        });


        Queue<double> temps = new Queue<double>();

        Queue<CPUState[]> states = new Queue<CPUState[]>();

        void RefreshData()
        {
            if (isdisposed) return;

            BlazorSession.Current.SetTimeout(priod, RefreshData);

            StateHasChanged();

            var newt = info.GetCPUTemp();
            BlazorSession.Current.ConsoleLog(newt);
            temps.Enqueue(newt);

            var laststate = info.GetCPUStates();
            states.Enqueue(laststate);

            if (newt > alertvalue && checkbox.Attribute_Checked)
                DoAlert();

            if (temps.Count > maxcount)
                temps.Dequeue();

            if (states.Count > maxcount)
                states.Dequeue();

            tempcanvas.ClearRect(0, 0, width, 200);
            tempcanvas.Font("12px Arial");
            tempcanvas.LineWidth(1);
            tempcanvas.StrokeText("70", 2, 12);
            tempcanvas.StrokeText("20", 2, 195);

            int index = 0;
            foreach (double tv in temps)
            {
                double x = width + (index - temps.Count) * width / maxcount;
                double y = 200 - (tv - 20) * 2 * 2;

                if (index == 0)
                {
                    tempcanvas.BeginPath();
                    tempcanvas.StrokeStyle("red");
                    tempcanvas.LineWidth(2);
                    tempcanvas.MoveTo(x, y);
                }
                else
                {
                    tempcanvas.LineTo(x, y);
                }
                tempcanvas.Stroke();
                index++;
            }

            tempcanvas.Flush();


            loadcanvas.ClearRect(0, 0, width, 200);


            index = 0;
            CPUState[] prevarr = null;
            foreach (CPUState[] statearr in states)
            {
                if (prevarr == null)
                {
                    prevarr = statearr;
                    continue;
                }

                int idle = statearr.Sum(v => v.Idle) - prevarr.Sum(v => v.Idle);
                int used = statearr.Sum(v => v.User + v.System) - prevarr.Sum(v => v.User + v.System);

                if (used + idle != 0)
                {
                    used = 100 * used / (used + idle);
                    lastused = used;
                }
                else
                {
                    used = lastused;
                }

                double x = width + (index - temps.Count) * width / maxcount;
                double y = 200 - used * 2;

                if (index == 0)
                {
                    loadcanvas.BeginPath();
                    loadcanvas.StrokeStyle("red");
                    loadcanvas.LineWidth(2);
                    loadcanvas.MoveTo(x, y);
                }
                else
                {
                    loadcanvas.LineTo(x, y);
                }
                loadcanvas.Stroke();

                prevarr = statearr;
                index++;

            }

            loadcanvas.Flush();
        }

        RefreshData();

    }
}
